//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "Audio/Loaders/AudioLoader.h"
#include "Shared/Resources/ResourceFileInfo.h"

namespace Engine
{
namespace Audio
{

#	include "Shared/Resources/SoundHeader.h"
	
	
/*
=================================================
	constructor
=================================================
*/
	AudioLoader::AudioLoader ()
	{}
	
/*
=================================================
	Register
=================================================
*/
	void AudioLoader::Register (ResourceManager *rm)
	{
		rm->RegisterLoader( EResource::AUDIO_SAMPLE, LoadFromFile, LoadFromName, this );
	}
	
/*
=================================================
	Unregister
=================================================
*/
	void AudioLoader::Unregister (ResourceManager *rm)
	{
		rm->UnregisterLoader( EResource::AUDIO_SAMPLE, LoadFromFile, LoadFromName, this );
	}

/*
=================================================
	LoadFromName
=================================================
*/
	bool AudioLoader::LoadFromName (ResourcePtr &resource, uni_c_string filename, void *parameter,
									const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		CHECK_ERR( loadParams.Is< AudioSample::AudioLoadParams >() );

		return _LoadFromName( resource, filename, parameter, loadParams.To< AudioSample::AudioLoadParams >(), ess );
	}
	
/*
=================================================
	LoadFromFile
=================================================
*/
	bool AudioLoader::LoadFromFile (ResourcePtr &resource, const RFilePtr &file, void *parameter,
									const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		CHECK_ERR( loadParams.Is< AudioSample::AudioLoadParams >() );

		return _LoadFromFile( resource, file, parameter, loadParams.To< AudioSample::AudioLoadParams >(), ess );
	}

/*
=================================================
	_LoadFromName
=================================================
*/
	bool AudioLoader::_LoadFromName (ResourcePtr &resource, uni_c_string filename, void *parameter,
									 const AudioSample::AudioLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		AudioSamplePtr	audio = AudioSample::New( ess );

		CHECK_ERR( audio->Load( filename ) );

		resource = audio;
		return true;
	}
	
/*
=================================================
	_LoadFromFile
=================================================
*/
	bool AudioLoader::_LoadFromFile (ResourcePtr &resource, const RFilePtr &file, void *parameter,
									 const AudioSample::AudioLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		AudioSamplePtr	audio = AudioSample::New( ess );
		
		RESFILE_CHECK_HEADER( file, SoundFileInfo );

		SoundHeader	header;
		CHECK_ERR( file->Read( header ) );

		EAudioFormat::type	fmt = EAudioFormat::UNKNOWN;

		switch ( header.fmt )
		{
			case SoundHeader::MP3 :		fmt = EAudioFormat::MP3;	break;
			case SoundHeader::OGG :		fmt = EAudioFormat::OGG;	break;
			case SoundHeader::WAV :		fmt = EAudioFormat::WAV;	break;
			default				  :		LOG( (string( "unknown format in file \"") << file->Name() << "\"").cstr(), ELog::ERROR );
		}

		CHECK_ERR( audio->Load( file, fmt ) );

		resource = audio;
		return true;
	}


}	// Audio
}	// Engine